package to3;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class voorraadBeheren2 extends javax.swing.JFrame {

    private OnderdelenLijst o1;
    private List<Onderdeel> oList;    
    private BestellingLijst b1;
    private List<Bestelling> bList;
    
    private OnderdelenLijst o2;
    private List<Onderdeel> oList2;    
    private BestellingLijst b2;
    private List<Bestelling> bList2;
    
    public voorraadBeheren2() {
        initComponents();
        setVisible(true);
        
                ////////SCHERM IN MIDDEN////////
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        this.setLocation(windowX, windowY);
        ///////////////////////////////
        
        o1 = new OnderdelenLijst();
        b1 = new BestellingLijst();
        oList = new ArrayList<>();
        bList = new ArrayList<>();
        
        o2 = new OnderdelenLijst();
        b2 = new BestellingLijst();
        oList2 = new ArrayList<>();
        bList2 = new ArrayList<>();
        try {
            readFile3();
        } catch (IOException ex) {
            Logger.getLogger(voorraadBeheren2.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            readFile4();
        } catch (IOException ex) {
            Logger.getLogger(voorraadBeheren2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tf_leveringsNummer = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btn_BevestigLevering = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        btn_BevestigLevering1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tf_leveringsNummer1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Levering bevestigen");

        jLabel2.setText("Leveringsnummer onderdelen:");

        btn_BevestigLevering.setText("Bevestig levering");
        btn_BevestigLevering.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_BevestigLeveringActionPerformed(evt);
            }
        });

        jButton5.setText("Hoofdmenu");
        jButton5.setPreferredSize(new java.awt.Dimension(164, 46));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        btn_BevestigLevering1.setText("Bevestig levering");
        btn_BevestigLevering1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_BevestigLevering1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Leveringsnummer brandstof:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(278, 278, 278)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(163, 163, 163)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btn_BevestigLevering)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(tf_leveringsNummer, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btn_BevestigLevering1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(tf_leveringsNummer1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(0, 310, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(127, 127, 127)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_leveringsNummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(btn_BevestigLevering)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_leveringsNummer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(btn_BevestigLevering1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        hoofdMenu hM = new hoofdMenu();
        this.setVisible(false);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void btn_BevestigLeveringActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_BevestigLeveringActionPerformed
        if(tf_leveringsNummer.getText().matches("[0-9]{8}")){
            System.out.println(b1.getBestelling(tf_leveringsNummer.getText()));
            if (b1.getBestelling(tf_leveringsNummer.getText()) != null) {
                JOptionPane.showMessageDialog(null, "Levering bevestigd", "Bevestigd", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Geen geldig leveringsnummer", "Fout", JOptionPane.ERROR_MESSAGE);
            } 
        } else {
                JOptionPane.showMessageDialog(null, "Geen geldig leveringsnummer", "Fout", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_BevestigLeveringActionPerformed

    private void btn_BevestigLevering1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_BevestigLevering1ActionPerformed
       if(tf_leveringsNummer.getText().matches("[0-9]{8}")){
            if (b2.getBestelling(tf_leveringsNummer.getText()) != null) {
                JOptionPane.showMessageDialog(null, "Levering bevestigd", "Bevestigd", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Geen geldig leveringsnummer", "Fout", JOptionPane.ERROR_MESSAGE);
            } 
        } else {
                JOptionPane.showMessageDialog(null, "Geen geldig leveringsnummer", "Fout", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_BevestigLevering1ActionPerformed

       private void readFile3() throws IOException {
                    b2.clearBestellingLijst();
                    FileReader fr = new FileReader("ProductBestellingen.txt");
                    BufferedReader br = new BufferedReader(fr);

                    while (true) {
                        String regel = br.readLine();
                        if (regel == null) {
                        break;
                    }
                    Scanner sc = new Scanner(regel);
                    sc.useDelimiter("\\s*,\\s*");
                    String bestelNummer = sc.next();
                    String omschrijving = sc.next();
                    String aantal = sc.next();
                    int aantalInt = Integer.parseInt(aantal);
                    b1.addBestelling(bestelNummer, omschrijving, aantalInt);
                    sc.close();
                    }
        }  
       
        private void readFile4() throws IOException {
                    b2.clearBestellingLijst();
                    FileReader fr = new FileReader("BrandstofBestellingen.txt");
                    BufferedReader br = new BufferedReader(fr);

                    while (true) {
                        String regel = br.readLine();
                        if (regel == null) {
                        break;
                    }
                    Scanner sc = new Scanner(regel);
                    sc.useDelimiter("\\s*,\\s*");
                    String bestelNummer = sc.next();
                    String omschrijving = sc.next();
                    String aantal = sc.next();
                    int aantalInt = Integer.parseInt(aantal);
                    b2.addBestelling(bestelNummer, omschrijving, aantalInt);
                    sc.close();
                    }
        } 
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_BevestigLevering;
    private javax.swing.JButton btn_BevestigLevering1;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField tf_leveringsNummer;
    private javax.swing.JTextField tf_leveringsNummer1;
    // End of variables declaration//GEN-END:variables
}
